package com.mz.ieann.aide.media.svga

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mz.ieann.aide.R
import com.mz.ieann.aide.base.BaseActivity
import com.mz.ieann.svgaplayer.*
import com.mz.ieann.svgaplayer.utils.log.SVGALogger
import java.io.File
import java.net.URL

/**
 * <p>------------------------------------------------------
 * <p>Copyright (C) 2020 wasu company, All rights reserved.
 * <p>------------------------------------------------------
 * <p>
 * <p>
 *
 * @author Created by zhangmz
 * @date on 2023/1/6
 */
class SvgaLoadTestActivity: BaseActivity() {

    private val SVGA_URL = "https://github.com/yyued/SVGA-Samples/blob/master/posche.svga?raw=true"

    private var currentIndex = 0
    private lateinit var mSVGAImageView: SVGAImageView
    private lateinit var svgaView: RecyclerView
    private val svgaList = ArrayList<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_svga_load_test_layout)
        svgaView = findViewById(R.id.svgaList)
        mSVGAImageView = findViewById(R.id.mSVGAImageView)

        mSVGAImageView.setOnClickListener {
            mSVGAImageView.stepToFrame(currentIndex++, false)
        }

        SVGAParser.shareParser().init(this)
        SVGALogger.setLogEnabled(true)
        SVGASoundManager.init()

        assets.list("svga")?.let {
            for (i in it.indices){
                svgaList.add("svga/${it[i]}")
            }
        }
        svgaList.add(SVGA_URL)

        for (i in svgaList.indices){
            Log.v("aaaa", "name:${svgaList[i]}")
        }

        svgaView.layoutManager = LinearLayoutManager(this)
        svgaView.isFocusableInTouchMode = true
        svgaView.itemAnimator = null
        val a = SVGAAdapter(this, svgaList,
            click = object : View.OnClickListener {
                override fun onClick(v: View?) {
                    SVGASoundManager.release()
                    val name = v?.tag as? String?: return
                    if (name.startsWith("http")){
                        loadSvgaUrl(name)
                    }else {
                        loadSvgaAssets(name)
                    }
                }
            }
        )
        svgaView.adapter = a
    }

    private fun loadSvgaAssets(
        name: String?
    ){
        name?.let {
            val svgaParser = SVGAParser.shareParser()
            svgaParser.setFrameSize(100, 100)
            svgaParser.decodeFromAssets(it, object : SVGAParser.ParseCompletion {
                override fun onComplete(videoItem: SVGAVideoEntity) {
                    Log.v("SVGAParser", "onComplete,videoItem:$videoItem")
                    mSVGAImageView.setVideoItem(videoItem)
                    mSVGAImageView.stepToFrame(0, true)
                }

                override fun onError() {
                    Log.e("SVGAParser", "error")
                }
            })
        }
    }

    private fun loadSvgaUrl(
        name: String?
    ){
        val svgaParser = SVGAParser.shareParser()
        svgaParser.decodeFromURL(
            url = URL(name),
            callback = object : SVGAParser.ParseCompletion {
                override fun onComplete(videoItem: SVGAVideoEntity) {
                    mSVGAImageView.setImageDrawable(SVGADrawable(videoItem))
                    mSVGAImageView.startAnimation()
                }
                override fun onError() {

                }
            },
            playCallback = object : SVGAParser.PlayCallback {
                override fun onPlay(file: List<File>) {

                }
            }
        )
    }

    private class SVGAAdapter(
        private val context: Context,
        private val list: List<String>,
        private val click: View.OnClickListener
    ): RecyclerView.Adapter<SVGAAdapter.SVGAHolder>(){

        override fun getItemCount(): Int = list.size

        override fun getItemId(position: Int) = position.toLong()

        override fun setHasStableIds(hasStableIds: Boolean) {
            super.setHasStableIds(true)
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SVGAHolder {
            val itemView = LayoutInflater.from(context).inflate(R.layout.adapter_svga_load_test_layout, null)
            return SVGAHolder(itemView)
        }

        override fun onBindViewHolder(holder: SVGAHolder, position: Int) {
            val text = list[holder.adapterPosition]
            holder.svgaName.text = text.substring(0, 20.coerceAtMost(text.length))
            holder.svgaName.tag = text
            holder.svgaName.setOnClickListener(click)
        }

        inner class SVGAHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val svgaName = itemView.findViewById<TextView>(R.id.svgaName)
        }
    }

}